package com.example.demo.service;

import com.example.demo.util.StoreUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Service
public class DynamicUrlService {

    public String createPath(String userId, String goodId){
        String key=userId+goodId;
        String value=RandomStringUtils.randomAlphanumeric(20);
        value=DigestUtils.md5DigestAsHex(value.getBytes(StandardCharsets.UTF_8));

        StoreUtil.setValue(key,value);
        return value;
    }

    public boolean checkPath(String pathKey, String pathValue){
        if (pathKey==null || pathValue==null){
            return false;
        }

        synchronized (this){
            if (pathValue.equals(StoreUtil.getValue(pathKey))){
                StoreUtil.deleteKey(pathKey);

                return true;
            }
        }

        return false;
    }
}
